
app.controller('warehouseDeployModalCtrl', ['$scope', 'httpLoad', '$rootScope','$state','$timeout','$stateParams','$sce',
     function($scope, httpLoad, $rootScope, $state, $timeout,$stateParams,$sce) {
       $rootScope.link = '/statics/css/sortware.css';
       $rootScope.moduleTitle =$sce.trustAsHtml('镜像管理 > <span>镜像</span> > 部署');
       $scope.envList = [{key:'',value:''}];
       $scope.labelList = [{keys:'',values:''}];
       $scope.name;  $scope.scriptItem = {};
	   ///
     (function(){

         var imageId = $stateParams.id;
       httpLoad.loadData({
           url:'/software/inspectImage',
           method:'POST',
           data: {imageId: imageId},
           success:function(data){
               if(data.success&&data.data){
                   $scope.supplierDetail = data.data;

                   //密码处理

               }
           }
       });
       })();
     $scope.param = {
              page:1,
              rows: 99999
            };
   	//应用列表
	$scope.getData = function (page) {
    //集群
    httpLoad.loadData({
       url: '/openshift/cluster/list',
      method: 'POST',
      data: $scope.param,
      noParam: true,
      success: function (data) {
        if (data.success) {
          $scope.masterData = data.data.rows;

        }
      }
    });
    //应用
			httpLoad.loadData({
	       url: '/application/list',
				method: 'POST',
				data: $scope.param,
				noParam: true,
				success: function (data) {
					if (data.success) {
						$scope.warehoseData = data.data.rows;

					}
				}
			});
		};

	$scope.getData(1);
       $scope.goBack = function(){
           $state.go('app.sortware.dockerimage');
       };

       $scope.addEnvs = function(){
                   $scope.envList.push({key:'',value:''})
        }
        $scope.addLabels = function(){
           //做验证-》只有上面用户组有用户下面才可以继续添加用户组
                    $scope.labelList.push({keys:'',values:''})
         }
        $scope.removeEnv = function(key){
          if($scope.envList.length == 1) return $scope.pop('请至少添加一组','error');
          $scope.envList.splice(key,1);
        }
        $scope.removeLabel = function(key){
          if($scope.labelList.length == 1) return $scope.pop('请至少添加一组','error');
          $scope.labelList.splice(key,1);
        }
        var id = $stateParams.id;

     $scope.ok = function () {
         var envList = {};
         var labelList = {};
         for(var i=0;i<$scope.envList.length;i++){
        	 var item = $scope.envList[i];
        	 if((!item.key&&item.value)||(item.key&&!item.value)){
                 $scope.pop('请添加完整的环境变量','error');
                 return;
              }else if(!item.key&&!item.value){
            	  continue
              }else{
                 envList[item.key] = item.value;
              }
         }
         for(var j=0;j<$scope.labelList.length;j++){
        	 var items = $scope.labelList[j];
        	 if((!items.keys&&items.values)||(items.keys&&!items.values)){
                 $scope.pop('请添加完整的标签','error');
                 return;
              }else if(!items.keys&&!items.values){
            	  continue
              }else{
            	  labelList[items.keys] = items.values;
              }
         }
        
       var param = {
         clusterId:$scope.clusterId,
        	   appId:$scope.scriptItem.selected.id,
    		   imageId:id,
    		   appName:$scope.name,
    		   envs:envList,
    		   labels:labelList,
    		   remark:$scope.remark
    		   };
          httpLoad.loadData({
               url: '/software/deployImage',
              method:'POST',
              data: param,
              success:function(data){
                  if(data.success){
                    	$scope.pop(data.message);
                	  $state.go('app.adhibition.apply');
                  }
              }
          });
      }

     }
 ]);
